package WebFinder

import (
	"gitee.com/fierce_wolf/go-web-crawler/WebCrawler"
	"regexp"
)

type CompanyName struct {
	List []*WebCrawler.WebElement // 家庭、办公地址的相关元素，其中text字段被识别为地址
}

func (e *CompanyName) GetList() []*WebCrawler.WebElement {
	return e.List
}

func (e *CompanyName) Find(els []*WebCrawler.WebElement) []*WebCrawler.WebElement {
	e.List = make([]*WebCrawler.WebElement, 0)

	// 查找长度是2-6个字符的元素
	reg := regexp.MustCompile(`([\p{Han}a-zA-Z0-9&·\-_]+(?:集团|有限|责任)?公司)`)
	els = WebCrawler.Site.FindElements(els, WebCrawler.Filter{
		Mode:  WebCrawler.ModeRegexp,
		Param: reg,
	})

	originals := make([]*WebCrawler.WebElement, 0)
	for _, el := range els {
		originals = append(originals, el)

		clone := el.Clone()
		e.List = append(e.List, clone)
	}

	return originals
}

func (e *CompanyName) FindTexts(text string) []string {
	regexp, _ := regexp.Compile(`([\p{Han}a-zA-Z0-9&·\-_]+(?:集团|有限|责任)?公司)`)
	return regexp.FindAllString(text, -1)
}
